class UnionFind:
    def __init__(self, n):
        self.fa = list(range(n + 1))
        self.size = [1] * (n + 1)

    def find(self, x):
        if self.fa[x] != x:
            self.fa[x] = self.find(self.fa[x])
        return self.fa[x]

    def union(self, x, y):
        root_x = self.find(x)
        root_y = self.find(y)
        if root_x == root_y:
            return False
        if self.size[root_x] < self.size[root_y]:
            self.fa[root_y] = root_x
            self.size[root_x] += self.size[root_y]
        else:
            self.fa[root_x] = root_y
            self.size[root_y] += self.size[root_x]
        return True


n, m, l, r = map(int, input().strip().split())
c2c = UnionFind(n)  # 可以相互连接的节点
c2b = UnionFind(n)  # 价值低于最低必须通过一个高价值的才能相互连
for k in range(m):
    u, v, w = map(int, input().split())
    if w <= r:
        c2c.union(u, v)
    if w < l:
        c2b.union(u, v)
result = 0
for i in range(1, n + 1):
    if c2c.find(i) == i:
        num = c2c.size[i]
        result += num * (num - 1) // 2
    if c2b.find(i) == i:
        num = c2b.size[i]
        result -= num * (num - 1) // 2
print(result)
